package com.ykzhujiang.lovespirit;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.ykzhujiang.lovespirit.analyzer.Analyzer;
import com.ykzhujiang.lovespirit.analyzer.FemaleAnalyzer;
import com.ykzhujiang.lovespirit.analyzer.MaleAnalyzer;
import com.ykzhujiang.lovespirit.analyzer.TimeAnalyzer;
import com.ykzhujiang.lovespirit.entity.LThread;
import com.ykzhujiang.lovespirit.entity.Notice;
import com.ykzhujiang.lovespirit.labor.FudanLabor;
import com.ykzhujiang.lovespirit.labor.Labor;
import com.ykzhujiang.lovespirit.labor.NanjingLabor;
import com.ykzhujiang.lovespirit.labor.NankaiLabor;
import com.ykzhujiang.lovespirit.labor.SJTULabor;
import com.ykzhujiang.lovespirit.labor.TsinghuaLabor;
import com.ykzhujiang.lovespirit.parser.FudanParser;
import com.ykzhujiang.lovespirit.parser.NanjingParser;
import com.ykzhujiang.lovespirit.parser.NankaiParser;
import com.ykzhujiang.lovespirit.parser.Parser;
import com.ykzhujiang.lovespirit.parser.SJTUParser;
import com.ykzhujiang.lovespirit.parser.TsinghuaParser;
import com.ykzhujiang.lovespirit.util.ParserProcessor;
import com.ykzhujiang.lovespirit.util.Utility;

@SuppressWarnings("serial")
public class LoveSpiritServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setCharacterEncoding("UTF8");
		resp.setContentType("text/plain");
		Labor l = null;
		Parser p = null;
		String type = req.getParameter("u");
		if (type != null) {
			if (type.equalsIgnoreCase("sjtu")) {
				l = new SJTULabor();
				p = new SJTUParser();
			} else if (type.equalsIgnoreCase("tsinghua")) {
				l = new TsinghuaLabor();
				p = new TsinghuaParser();
			} else if (type.equalsIgnoreCase("nju")) {
				l = new NanjingLabor();
				p = new NanjingParser();
			} else if (type.equalsIgnoreCase("nankai")) {
				l = new NankaiLabor();
				p = new NankaiParser();
			} else if (type.equalsIgnoreCase("fudan")) {
				l = new FudanLabor();
				p = new FudanParser();
			}
			Start(l, p, new TimeAnalyzer(7 * 3600000L * 12), null/*
																 * resp.getWriter
																 * ()
																 */);
			resp.getWriter().println("success");
		} else {
			logger.warning("no param");
		}

		Notice notice=null;
		MaleAnalyzer ma=new MaleAnalyzer();
		FemaleAnalyzer fa=new FemaleAnalyzer();
		LThread lt=new LThread();
		lt.notices.clear();
		lt.notices.add(notice);
		if(ma.Analyze(lt));
		else if(fa.Analyze(lt));
		
//		 try {
//			PersistenceManager pm = PMF.get().getPersistenceManager();
//			 Query query = pm.newQuery(Notice.class, "hot>10 order by hot desc");
//			
//			 List<Notice> results = (List<Notice>) query.execute();
//			 resp.getWriter().println(results.size());
//			 pm.close();
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	public static void Start(Labor l, Parser p, Analyzer a, PrintWriter pw) {
		if (l == null || p == null || a == null)
			return;
		l.Fetch(1);
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("threads");
		for (int i = 0; i < l.files.size(); i++) {
			ParserProcessor pp = new ParserProcessor(p, root, (String) l.files
					.get(i));
			pp.AddAnalyzer(a);
			pp.SetWriter(pw);
			pp.run();
		}
	}

	private static final Logger logger = Logger.getLogger(LoveSpiritServlet.class
			.getName());
}
